package cn.openea.perpartner.meily.web.entity.dto;

import cn.openea.perpartner.common.core.annotation.Excel;
import cn.openea.perpartner.meily.web.entity.Customer;
import cn.openea.perpartner.meily.web.entity.Provider;
import cn.openea.perpartner.meily.web.service.IPersonalInfoService;
import cn.openea.perpartner.meily.web.service.IProviderService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.function.Function;

/**
 * @author ysk
 * @version 1.0.1
 * @Description meily 平台用户信息
 * @createTime 2023年04月27日 22:36:00
 */
@Component
public class CustomerDTOFactory {


    @Resource
    private EvaluatorPeopleDTOFactory evaluatorPeopleDTOFactory;
    @Resource
    private IProviderService providerService;
    @Resource
    private IPersonalInfoService personalInfoService;

    /**
     * 用户基本信息DTO
     */
    @Data
    @SuperBuilder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class CustomerBasicInfoDTO {
        private Long id;
        /**
         * 用户id 唯一
         */
        private String userId;
        /**
         * 用户昵称
         */
        private String userName;
        /**
         * 真实姓名
         */
        private String realName;
        /**
         * 用户头像
         */
        private String avatarUrl;
        /**
         * 性别
         */
        private String sex;
        /**
         * 年龄层
         */
        private String ageGroup;
        /**
         * 身份职业
         */
        private String identityJob;
        /**
         * 月收入
         */
        private String monthlyIncome;
        /**
         * 个人介绍
         */
        private String introduction;
        /**
         * 手机号码
         */
        private String phoneNumber;
        /**
         * 所在地区
         */
        private String region;
        /**
         * 街道乡镇
         */
        private String streetVillage;
        /**
         * 详细地址
         */
        private String address;
        /**
         * 微信唯一id
         */
        private String openid;
        /**
         * 小程序和公众号的唯一id
         */
        private String unionid;
        /**
         * 公众号的openid
         */
        private String subscriptionOpenid;
        /**
         * 是否关注公众号0 未关注 1 已关注
         */
        private Integer isSubscribed;
        /**
         * 区域服务商编号
         */
        private String providerId;
        /**
         * 测评师编号
         */
        private String evaluatorId;
        /**
         * 平台身份
         */
        private String platformIdentity;
        private String createTime;
        private String updateTime;
    }

    public Function<Customer, CustomerBasicInfoDTO> toBasicInfoDTO = (customer) -> {
        if (customer == null) {
            return null;
        }
        CustomerBasicInfoDTO basicInfoDTO = new CustomerBasicInfoDTO();
        BeanUtils.copyProperties(customer, basicInfoDTO);
        // 平台身份
        // 注册区域服务商就是Q，注册测评师就是C，其余就是N。如果之前是N，但后来注册了区域服务商就是Q，再注册测评师就是Q/C
        String platformIdentity = "";
        if (StringUtils.isNotBlank(customer.getProviderId()) && StringUtils.isNotBlank(customer.getEvaluatorId())){
            platformIdentity = "服+测";
        }
        if (StringUtils.isNotBlank(platformIdentity)){
            basicInfoDTO.setPlatformIdentity(platformIdentity);
            return basicInfoDTO;
        }
        if (StringUtils.isNotBlank(customer.getProviderId())) {
            platformIdentity = "服务商";
        }
        if (StringUtils.isNotBlank(customer.getEvaluatorId())) {
            platformIdentity = "测评师";
        }
        basicInfoDTO.setPlatformIdentity(platformIdentity);
        return basicInfoDTO;
    };

    /**
     * 用户所有信息DTO 包含测评师和区域服务商等信息
     */
    @Data
    @SuperBuilder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class CustomerAllInfoDTO {
        // 基本信息
        private CustomerBasicInfoDTO customerBasicInfoDTO;
        // 测评师
        private EvaluatorPeopleDTOFactory.EvaluatorPeopleBasicInfoDTO evaluatorPeopleBasicInfoDTO;
        // 区域服务商
        private Provider providerDTO;

    }


    public Function<Customer, CustomerAllInfoDTO> toAllInfoDTO = (customer) -> {
        if (customer == null) {
            return null;
        }
        EvaluatorPeopleDTOFactory.EvaluatorPeopleBasicInfoDTO evaluatorPeopleBasicInfoDTO = null;
        if (StringUtils.isNotBlank(customer.getEvaluatorId())) {
            evaluatorPeopleBasicInfoDTO = evaluatorPeopleDTOFactory.toBasicInfoDTO.apply(personalInfoService.getById(customer.getEvaluatorId()));
        }
        Provider provider = null;
        if (StringUtils.isNotBlank(customer.getProviderId())) {
            provider = providerService.getById(customer.getProviderId());
        }
        return CustomerAllInfoDTO.builder()
                .customerBasicInfoDTO(toBasicInfoDTO.apply(customer))
                .evaluatorPeopleBasicInfoDTO(evaluatorPeopleBasicInfoDTO)
                .providerDTO(provider)
                .build();
    };

    /**
     * 用户信息导出
     */
    @Data
    @SuperBuilder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class CustomerExportExcelDTO {
        @Excel(name = "平台编号")
        private String userId;
        @Excel(name = "平台身份")
        private String platformIdentity;
        @Excel(name = "注册时间")
        private String createTime;
        @Excel(name = "用户昵称")
        private String userName;
        @Excel(name = "真实姓名")
        private String realName;
        @Excel(name = "用户头像")
        private String avatarUrl;
        @Excel(name = "性别")
        private String sex;
        @Excel(name = "年龄层")
        private String ageGroup;
        @Excel(name = "个人介绍")
        private String introduction;
        @Excel(name = "身份职业")
        private String identityJob;
        @Excel(name = "月收入")
        private String monthlyIncome;
        @Excel(name = "手机号码")
        private String phoneNumber;
        @Excel(name = "所在地区")
        private String region;
        @Excel(name = "街道乡镇")
        private String streetVillage;
        @Excel(name = "详细地址")
        private String address;
        @Excel(name = "绑定公众号（是/否）", readConverterExp = "0=否,1=是")
        private Integer isSubscribed;
        @Excel(name = "测评师（是/否）")
        private String isEvaluatorPeople;
        @Excel(name = "区域服务商（是/否）")
        private String isProvider;

    }


    public Function<Customer, CustomerExportExcelDTO> toExportExcelDTO = (customer) -> {
        if (customer == null) {
            return null;
        }
        CustomerExportExcelDTO exportExcelDTO = new CustomerExportExcelDTO();
        BeanUtils.copyProperties(customer, exportExcelDTO);
        exportExcelDTO.setIsEvaluatorPeople(StringUtils.isNotBlank(customer.getEvaluatorId()) ? "是" : "否");
        exportExcelDTO.setIsProvider(StringUtils.isNotBlank(customer.getProviderId()) ? "是" : "否");
        // 平台身份
        // 注册区域服务商就是Q，注册测评师就是C，其余就是N。如果之前是N，但后来注册了区域服务商就是Q，再注册测评师就是Q/C
        String platformIdentity = "";
        if (StringUtils.isNotBlank(customer.getProviderId()) && StringUtils.isNotBlank(customer.getEvaluatorId())){
            platformIdentity = "服+测";
        }
        if (StringUtils.isNotBlank(platformIdentity)){
            exportExcelDTO.setPlatformIdentity(platformIdentity);
            return exportExcelDTO;
        }
        if (StringUtils.isNotBlank(customer.getProviderId())) {
            platformIdentity = "服务商";
        }
        if (StringUtils.isNotBlank(customer.getEvaluatorId())) {
            platformIdentity = "测评师";
        }
        exportExcelDTO.setPlatformIdentity(platformIdentity);
        return exportExcelDTO;
    };

}
